﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using GPI.Server;
using Model;
using System.Text;
using Moon.Common.WebInfo;
using Moon.Common;
using System.IO;

public partial class Admin_ContentManage_SinglePage : System.Web.UI.Page
{
    NodeService server = new NodeService();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["id"] != null)
            {
               
                //获得查询的ID号
                string id = Request.QueryString["id"].ToString();
                this.lbnEditNode.CommandArgument = id;
                this.lbnEditContent.CommandArgument = id;
                NodeInfo model = server.GetModel(id.ToInt(0));
                this.labTitle.Text = model.NodeName;
                //绑定列表
                this.repNodeList.DataSource = server.GetAllListByCh(id);
                this.repNodeList.DataBind();
                //绑定内容编辑页面
                this.txtContent.Text = model.Content;
                this.hfImage.Value = model.NodeImage;
            }
            else
            {
                //this.form1.Visible = false;
                this.form1.InnerText = "404 Not Found!";
            }
        }
    }

    protected void lbnEditNode_Click(object sender, EventArgs e)
    {
        string id = this.lbnEditNode.CommandArgument;
        //绑定列表
        this.repNodeList.DataSource = server.GetAllListByCh(id);
        this.repNodeList.DataBind();
        this.MultiView1.ActiveViewIndex = 0;
    }
    //编辑按钮
    protected void lbnEditContent_Click(object sender, EventArgs e)
    {
        this.MultiView1.ActiveViewIndex = 1;
        if (this.txtContent.Text.IsNullOrEmpty())
        {
            string id = this.lbnEditContent.CommandArgument;
            NodeInfo model = server.GetModel(id.ToInt(0));
            //绑定内容编辑页面
            this.txtContent.Text = model.Content;
        }
    }
    //返回更新用的Url
    public string getUrl(object tmp, object id)
    {
        ListItem obj = SystemConfigInfo.TypeMap.FindByText(tmp.ToString());

        return obj != null ? obj.Value + id :"";
    }
    //保存模板
    protected void btnSaveTemplate_Click(object sender, EventArgs e)
    {
        //获得内容编辑的ID号
        string id = this.lbnEditContent.CommandArgument;
        //获得编辑内容
        NodeInfo model = server.GetModel(id.ToInt(0));
        if (!model.IsHTML)
        {
            model.Url = WebConfigInfo.ShowAction + model.Id;
            model.Content = this.txtContent.Text;
            if (this.fuImage.HasFile)
                model.NodeImage = WebConfigInfo.ImgPath + Guid.NewGuid().ToString() + this.fuImage.FileName.GetFileExt();
            else
                model.NodeImage = this.hfImage.Value;
            if (uPFile(this.fuImage,model.NodeImage.GetFileName(),Application["WebPath"]+WebConfigInfo.ImgPath,WebConfigInfo.Imgformat)&&server.Update(model))
                MessageBox.Show(this.Page, "操作成功！");
            else
                MessageBox.Show(this.Page, "操作失敗！");
        }
        else
        { }
        this.MultiView1.ActiveViewIndex = 0;
        //绑定列表
        this.repNodeList.DataSource = server.GetAllListByCh(id);
        this.repNodeList.DataBind();
    }


    /// <summary>
    /// 上傳操作
    /// </summary>
    /// <param name="Fupload">上傳空間</param>
    /// <param name="SaveName">保存名</param>
    /// <param name="dir">保存目錄</param>
    /// <param name="CheckType">驗證類型字符串</param>
    /// <returns></returns>
    private bool uPFile(System.Web.UI.WebControls.FileUpload Fupload, string SaveName, string dir, string CheckType)
    {

        /*如果上傳控件存在文件*/
        if (!this.hfImage.Value.IsNullOrEmpty())
        {
            CoFileHepler.DeleteFile(Application["WebPath"] + this.hfImage.Value);
        }
            
            /*
             * 判斷目錄是否存在不存在則進行創建
             */
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            try
            {
                //獲得文件類型
                string type = CoFileHepler.GetPostfixStr(Fupload.FileName);
                //判斷文件類型是否為需要的類型
                if (CoFileHepler.CheckValidExt(type, CheckType))
                {
                    Fupload.PostedFile.SaveAs(dir + SaveName);
                }
                return true;
            }
            catch
            {
                return false;
            }
    }
}
